#-*-coding:UTF-8-*-

import hashlib
import uuid
import hmac

from ..config import get_property

_SESSION_CONTAINER={}


class SessionData(dict):

    def __init__(self, session_id, hmac_key):
        self._session_id = session_id
        self._hmac_key = hmac_key
        #self.username=None
        self._username = None
        self._userid = None
        #self._is_user_admin = False
        self._content = None

    def get_session_id(self):
        return self._session_id
    
    def get_userid(self):
        #if not self._userid:
        #    raise Exception('user not login')
        return self._userid

    def set_user(self, user_id, user_name):
        if user_id and user_name:
            self._userid = user_id
            self._username = user_name
        else:
            raise Exception(
                'user ID(%s) and user name(%s) should not be none'\
                %s(user_id, user_name))

    def get_username(self):
        #if not self._username:
        #    raise Exception('user not login')
        return self._username

    def set_attribute(self, key, value):
        if not self._content:
            self._content = dict()
        self._content[key] = value

    def get_attribute(self, key):
        if self._content and self._content.has_key(key):
            return self._content[key]
        else:
            return

    def pop_attribute(self, key):
        if self._content and self._content.has_key(key):
            return self._content.pop(key)
        else:
            return


    '''
    def set_username(self, username):
        self._username=username

    def set_userid(self, userid):
        self._userid = userid
    
    def is_user_admin(self):
        return self._is_user_admin

    def set_user_admin(self):
        self._is_user_admin=True
    '''


def _generate_id():
    new_id=hashlib.sha256(get_property('session_secret')+str(uuid.uuid4()))
    return new_id.hexdigest()


def _generate_hmac(session_id):
    return hmac.new(session_id, get_property('session_secret'),hashlib.sha256).hexdigest()


def get_session(request):
    global _SESSION_CONTAINER
    sid_cookie = request.get_secure_cookie('TORNADOSESSIONID')
    if sid_cookie is not None and _SESSION_CONTAINER.has_key(sid_cookie):
        return _SESSION_CONTAINER[sid_cookie]
    sid = _generate_id()
    hmac_key = _generate_hmac(sid)
    _SESSION_CONTAINER[sid] = SessionData(sid, hmac_key)
    request.set_secure_cookie('TORNADOSESSIONID', sid)
    return _SESSION_CONTAINER[sid]

'''
def create_session(request):
    global _SESSION_CONTAINER
    sid_cookie=request.get_secure_cookie('TORNADOSESSIONID')
    if sid_cookie!=None and _SESSION_CONTAINER.has_key(sid_cookie):
        return _SESSION_CONTAINER[sid_cookie]
    sid=_generate_id()
    hmac_key=_generate_hmac(sid)
    _SESSION_CONTAINER[sid]=SessionData(sid,hmac_key)
    request.set_secure_cookie('TORNADOSESSIONID',sid)
    return _SESSION_CONTAINER[sid]
'''

def remove_session(request):
    global _SESSION_CONTAINER
    if _SESSION_CONTAINER.has_key(request.get_session().get_session_id()):
        del _SESSION_CONTAINER[request.get_session().get_session_id()]
    request.set_secure_cookie('TORNADOSESSIONID', '')

